const enterpriseVue = new Vue({
  el: '#app',
  data() {
    //校验身份证号
    return {
      form: {
        template_id:"",
        template_name:"",
        template_type:"0",
        App_ID:"",
        // Children:{
        //   wechat_template_detail:[{
        //     Enterprise_Info_SerialNumber:""
        //   }]
        // }
      },
      rules: [{
        template_id:[{required: true, message: '请输入模板id', trigger: 'blur'}],
        template_name:[{ required: true, message: '请输入模板名称', trigger: 'blur'}],
        App_ID:[{required:true, message: '请选择所属公众号或小程序', trigger:'change'}],
      }],
      enterpriseData:[],
      App_ID:[],
      loading: false,
      disabled: false,
    }
  },
  mounted() {
    this.getData()
    this.getAppID()
  },
  methods: {
     //查询所有企业
    getData() {
      let _this = this
      $.ajax({
        type: 'GET',
        url: DomainName + '/getEnterpriseInfoAction?token=' + $.cookie('token'),
        success: function (data) {
          _this.enterpriseData = data?.Data;
        },
        dataType: "json"
      });
      
    },
    getAppID(){
      this.form.App_ID = ''
      let _this = this
      $.ajax({
        type: 'POST',
        url: DomainName + '/getInquiryIDAction?token=' + $.cookie('token'),
        data:{
          Data:'enterprise_appid',
          Primary_Key:this.form.template_type,
          Primary_Key_Name:'App_Type',
          isNullEnterprise:true
        },
        success: function (data) {
          console.log(data);
          _this.App_ID = data;
        },
        dataType: "json"
      });
    },
    add_wechat_template_detail(){
      this.form.Children.wechat_template_detail.push({
        Enterprise_Info_SerialNumber: '',
      });
    },
    delet_wechat_template_detail(){
      if(this.form.Children.wechat_template_detail.length > 1){
        this.form.Children.wechat_template_detail.pop();
      }else{
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    submitForm(type) {
      const _this = this;
      this.$refs[type].validate((valid) => {
        if (valid) {
          _this.loading = true
          console.log(_this.form);
          $.ajax({
            url: DomainName + '/getAddAction?token='+$.cookie("token"),
            data:{
              Data: JSON.stringify({ wechat_template: [_this.form] }),
            },
            success: function (res) {
              console.log(res);
              if (res.Status == 'success') {
                _this.$message.success("提交成功")
                window.history.go(-1);
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      })
    }
  }
})